package com.taoyibao.hongcanting.ui.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.dialog.DialogComment;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.BalanceModel;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.UserInfoModel;
import com.taoyibao.hongcanting.ui.mine.delegate.WalletDelegate;
import com.taoyibao.hongcanting.utils.CodeUtils;

import java.util.List;

/**
 * @author TangAnna
 * @description: 钱包
 * @date :${DATA} 9:46
 */
public class WalletActivity extends BaseActivityPresenter<WalletDelegate> implements View.OnClickListener {
    @Override
    protected Class<WalletDelegate> getDelegateClass() {
        return WalletDelegate.class;
    }

    public static void open(Context context) {
        Intent intent = new Intent(context, WalletActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.get(R.id.tv_wallet_withdraw).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserBalance();
    }

    /**
     * 获取用户余额
     */
    public void getUserBalance() {
        JsonApiWrapper.getApiService().userBalance(SpUtils.getToken(), SpUtils.getMemberId())
                .compose(new DefaultTransformer<CompanyBaseModel<List<BalanceModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<BalanceModel>>>(this, false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<BalanceModel>> balanceModel) {
                        if (balanceModel.code == NetCode.HTTP_200) {
                            viewDelegate.setBalanceUI(balanceModel.data);
                        }else {
                            super.onNext(balanceModel);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_wallet_withdraw://提现
                isVerified();
                break;
        }
    }

    /**
     * 判断实名认证状态
     */
    public void isVerified() {
        JsonApiWrapper.getApiService().userInfo(SpUtils.getToken(), SpUtils.getMemberId())
                .compose(new DefaultTransformer<CompanyBaseModel<List<UserInfoModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<UserInfoModel>>>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel<List<UserInfoModel>> listCompanyBaseModel) {
                        if (listCompanyBaseModel.code == NetCode.HTTP_200 && listCompanyBaseModel.data != null && listCompanyBaseModel.data.size() > 0) {
                            UserInfoModel userInfoModel = listCompanyBaseModel.data.get(0);
                            if (userInfoModel.user_verify != null) {
                                if (userInfoModel.user_verify.status == CodeConstant.VERIFY_FAIL) {//认证失败
                                    showDialog(CodeUtils.getIdString(R.string.dialog_verifiedFail), CodeConstant.VERIFY_FAIL);
                                } else if (userInfoModel.user_verify.status == CodeConstant.VERIFY_WAIT) {//审核中
                                    showDialog(CodeUtils.getIdString(R.string.dialog_verifiedWait), CodeConstant.VERIFY_WAIT);
                                } else if (userInfoModel.user_verify.status == CodeConstant.VERIFY_PARSS) {//通过
                                    if (userInfoModel.pay_pwd == CodeConstant.SET_PAY_PWD_YES) {//设置支付密码了
                                        nextWithdraw();
                                    } else if (userInfoModel.pay_pwd == CodeConstant.SET_PAY_PWD_NO) {//未设置支付密码
                                        showDialog(CodeUtils.getIdString(R.string.dialog_setPayPwd), CodeConstant.SET_PAY_PWD_NO);
                                    }
                                } else {//也算没有实名认证
                                    showDialog(CodeUtils.getIdString(R.string.dialog_verifiedNot), CodeConstant.VERIFY_NOT);
                                }
                            } else {//未进行实名认证 弹窗进行实名认证
                                showDialog(CodeUtils.getIdString(R.string.dialog_verifiedNot), CodeConstant.VERIFY_NOT);
                            }
                        } else {
                            super.onNext(listCompanyBaseModel);
                        }

                    }
                });
    }

    /**
     * 判断是否设置支付密码
     */
    public void isSetPayPwd() {

    }

    public void nextWithdraw() {
        startActivity(new Intent(this, WithdrawActivity.class));
    }

    /**
     * 显示弹窗
     */
    public void showDialog(String text, int tag) {
        mConfirmTag = tag;
        DialogComment dialogComment = new DialogComment(text, onConfirm);
        dialogComment.showDilaog(getSupportFragmentManager(), "TAG");
    }

    /**
     * 用于判断确认按钮进行什么操作
     * 0:
     */
    private int mConfirmTag;
    DialogComment.DialogCommentInterfaca onConfirm = new DialogComment.DialogCommentInterfaca() {
        @Override
        public void onConfirmListener() {
            //TODO 跳转实名认证页面
            switch (mConfirmTag) {
                case CodeConstant.VERIFY_FAIL://认证失败
                case CodeConstant.VERIFY_WAIT://审核中
                case CodeConstant.VERIFY_NOT://未认证
                    VerifiedActivity.open(WalletActivity.this);
                    break;
                case CodeConstant.SET_PAY_PWD_NO://未设置支付密码
                    SetPayPwdStepOneActivity.open(WalletActivity.this, CodeUtils.getIdString(R.string.string_setPayPwd));
                    break;
            }
        }
    };
}
